<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
 * App\Models\UserCar
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $brand_id 品牌ID
 * @property string $brand_name 车辆品牌
 * @property string $type 车辆类型
 * @property string $plate_number 车牌号
 * @property string $color 车身颜色
 * @property string $name 联系人
 * @property string $phone 联系方式
 * @property int $is_default 是否默认
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static Builder|UserCar newModelQuery()
 * @method static Builder|UserCar newQuery()
 * @method static Builder|UserCar query()
 * @method static Builder|UserCar whereBrandId($value)
 * @method static Builder|UserCar whereColor($value)
 * @method static Builder|UserCar whereCreatedAt($value)
 * @method static Builder|UserCar whereId($value)
 * @method static Builder|UserCar whereName($value)
 * @method static Builder|UserCar wherePhone($value)
 * @method static Builder|UserCar wherePlateNumber($value)
 * @method static Builder|UserCar whereType($value)
 * @method static Builder|UserCar whereUpdatedAt($value)
 * @method static Builder|UserCar whereUserId($value)
 * @mixin \Eloquent
 * @property-read \App\Models\CarBrand|null $brand
 * @method static Builder|UserCar whereBrandName($value)
 * @method static Builder|UserCar whereIsDefault($value)
 */
class UserCar extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'user_cars';

    protected $fillable = [
        'user_id','brand_id','brand_name','type','plate_number',
        'color','name','phone','is_default'
    ];

    protected static function booted()
    {
        self::creating(function (UserCar $car){
            $exists = self::query()->where("user_id", $car->user_id)->exists();
            if (!$exists){
                $car->is_default = 1;
            }
        });
        self::saving(function (UserCar $car){
            if ($car->brand_id){
                $brand = CarBrand::find($car->brand_id);
                if ($brand){
                    $car->brand_name = $brand->name;
                }
            }
        });
        self::saved(function(UserCar $row){
            if ($row->is_default){
                self::query()
                    ->where("id","<>",$row->id)
                    ->where("user_id",$row->user_id)
                    ->update(['is_default'=>0]);
            }
        });
        self::deleted(function (UserCar $row){
            if ($row->is_default){
                self::query()
                    ->where("user_id",$row->user_id)
                    ->orderByDesc("id")
                    ->limit(1)
                    ->update(['is_default'=>1]);
            }
        });
    }


    public function brand(): HasOne
    {
        return $this->hasOne(CarBrand::class,"id","brand_id");
    }

    public function user(): HasOne
    {
        return $this->hasOne(User::class,"id","user_id");
    }



    public static function getList($user_id): Collection|array
    {
        return self::query()->where("user_id",$user_id)->with(['brand'=>function($query){
            $query->select(['id','name']);
        }])->orderByDesc("id")->get();
    }

    public static function getDetail($id): Model|Builder|null
    {
        return self::query()->where("id",$id)->first();
    }

}
